package com.zhijian.medical.service.impl;

import com.zhijian.medical.config.Constants;
import com.zhijian.medical.dao.remote.HarmfulMapper;
import com.zhijian.medical.dao.remote.MedicalDeviceMapper;
import com.zhijian.medical.dao.remote.StoneMapper;
import com.zhijian.medical.entity.vo.request.MedicalDeviceSearchVo;
import com.zhijian.medical.entity.vo.request.StoneSearchVo;
import com.zhijian.medical.entity.vo.response.HarmfulVo;
import com.zhijian.medical.entity.vo.response.MedicalDeviceVo;
import com.zhijian.medical.entity.vo.response.StoneVo;
import com.zhijian.medical.enums.ResponseCodeEnum;
import com.zhijian.medical.exception.BusinessException;
import com.zhijian.medical.service.LocationService;
import com.zhijian.medical.service.SensorService;
import com.zhijian.medical.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Service
public class SensorServiceImpl implements SensorService {

    @Autowired
    private MedicalDeviceMapper mapper;

    @Autowired
    private StoneMapper stoneMapper;

    @Autowired
    private HarmfulMapper harmfulMapper;


    @Autowired
    private LocationService locationService;

    public Integer count(MedicalDeviceSearchVo deviceSearchVo) {
        return mapper.count(deviceSearchVo);
    }

    @Override
    public Page<StoneVo> selectStonePage(StoneSearchVo deviceSearchVo) {
        return null;
    }

    public List<MedicalDeviceVo> selectList(MedicalDeviceSearchVo deviceSearchVo) {
        List<MedicalDeviceVo> deviceVos = new ArrayList<>();
        if(deviceSearchVo.getLocationId() == null){
            deviceVos = mapper.selectList(deviceSearchVo);
        }else{
            deviceVos = mapper.selectListHarmful(deviceSearchVo);

        }


        if (!deviceVos.isEmpty()) {
            List<MedicalDeviceVo> result = new LinkedList<>();
            deviceVos.forEach(vo -> {
                vo.setLocation(locationService.concatNameByDeepId(vo.getLocationId()).toString());
                result.add(vo);
            });
            return result;
        }
        return deviceVos;
    }

    public List<MedicalDeviceVo> selectList2(MedicalDeviceSearchVo deviceSearchVo) {

        List<MedicalDeviceVo> deviceVos = mapper.selectList2(deviceSearchVo);

        if (!deviceVos.isEmpty()) {
            List<MedicalDeviceVo> result = new LinkedList<>();
            deviceVos.forEach(vo -> {
                vo.setLocation(locationService.concatNameByDeepId(vo.getLocationId()).toString());
                result.add(vo);
            });
            return result;
        }
        return deviceVos;
    }
    public List<StoneVo> selectStoneList(MedicalDeviceSearchVo deviceSearchVo) {

        return stoneMapper.selectList(deviceSearchVo);
    }

    public Integer stoneCount(MedicalDeviceSearchVo deviceSearchVo) {
        return stoneMapper.count(deviceSearchVo);
    }



//    public Page<StoneVo> selectStonePage(StoneSearchVo deviceSearchVo) {
//        //先直接查总数
//        int count = stoneCount(deviceSearchVo);
//        //分页
//        Page<StoneVo> page = new Page<>(true, count, deviceSearchVo.getPageNum(), deviceSearchVo.getPageSize());
//        deviceSearchVo.setStartIndex(page.getStartIndex());
//        page.setPage(true);
//        page.setTotalCount(count);
//        List<StoneVo> responseVos = this.selectStoneList(deviceSearchVo);
//        page.setData(responseVos);
//        return page;
//    }

    @Override
    public Page<MedicalDeviceVo> selectEveryPage(MedicalDeviceSearchVo deviceSearchVo) {
        if (deviceSearchVo.getPageNum() < 1 || deviceSearchVo.getPageSize() < 1) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "分页参数不合法");
        }

        if (null != deviceSearchVo.getLocationId() && !Objects.equals(deviceSearchVo.getLocationId(), Constants.Location.NO_LOCATION)) {
            //选择一个位置后，要选他所有的子级区域，除非是-1.-1代表没有分配位置
            if(deviceSearchVo.getLocationId() !=0){
                deviceSearchVo.setLocationIds(locationService.concatIdsByParentId(deviceSearchVo.getLocationId()));
                deviceSearchVo.setLocationId(null);
            }
        }

        //先直接查总数
        int count = count(deviceSearchVo);
        //分页
        Page<MedicalDeviceVo> page = new Page<>(true, count, deviceSearchVo.getPageNum(), deviceSearchVo.getPageSize());
        deviceSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<MedicalDeviceVo> responseVos = this.selectList(deviceSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public Page<MedicalDeviceVo> selectPage(MedicalDeviceSearchVo deviceSearchVo) {
        if (deviceSearchVo.getPageNum() < 1 || deviceSearchVo.getPageSize() < 1) {
            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "分页参数不合法");
        }

        if (null != deviceSearchVo.getLocationId() && !Objects.equals(deviceSearchVo.getLocationId(), Constants.Location.NO_LOCATION)) {
            //选择一个位置后，要选他所有的子级区域，除非是-1.-1代表没有分配位置
            if(deviceSearchVo.getLocationId() !=0){
                deviceSearchVo.setLocationIds(locationService.concatIdsByParentId(deviceSearchVo.getLocationId()));
                deviceSearchVo.setLocationId(null);
            }
        }

        //先直接查总数
        int count = count(deviceSearchVo);
        //分页
        Page<MedicalDeviceVo> page = new Page<>(true, count, deviceSearchVo.getPageNum(), deviceSearchVo.getPageSize());
        deviceSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<MedicalDeviceVo> responseVos = this.selectList(deviceSearchVo);
        for(MedicalDeviceVo vo:responseVos){
            if(vo.getDeviceType() == 1){
                List<String> list = harmfulMapper.queryStatus(vo.getDeviceId());
                if(list.size()==1&&list.get(0).equals("1")){
                    vo.setHealthStatus("健康");
                }else{
                    vo.setHealthStatus("异常");
                }
            }else{
                vo.setHealthStatus("健康");
            }
        }
        page.setData(responseVos);
        return page;
    }


    public Integer harmfulCount(MedicalDeviceSearchVo deviceSearchVo) {
        return harmfulMapper.count(deviceSearchVo);
    }

    public List<HarmfulVo> selectHarmfulList(MedicalDeviceSearchVo deviceSearchVo) {
        return harmfulMapper.selectList(deviceSearchVo);
    }

    @Override
    public Page<HarmfulVo> selectByDetails(MedicalDeviceSearchVo deviceSearchVo) {
        //先直接查总数
        Page<HarmfulVo> page = new Page<HarmfulVo>();
        int count = harmfulCount(deviceSearchVo);
        List<HarmfulVo> responseVos  = new ArrayList<>();
        if(count == 0) {
             count = stoneCount(deviceSearchVo);
            page= new Page<>(true, count, deviceSearchVo.getPageNum(), deviceSearchVo.getPageSize());
            deviceSearchVo.setStartIndex(page.getStartIndex());
            page.setPage(true);
            page.setTotalCount(count);
            List<StoneVo>  responseVos1 = this.selectStoneList(deviceSearchVo);
            for(StoneVo stoneVo :responseVos1){
                HarmfulVo harmfulVo = HarmfulVo.builder()
                        .deviceId(stoneVo.getAddress())
                        .gasStatus(1)
                        .gasValue(stoneVo.getVal())
                        .build();
                responseVos.add(harmfulVo);
            }
            page.setData(responseVos);
        }else{
            page = new Page<>(true, count, deviceSearchVo.getPageNum(), deviceSearchVo.getPageSize());
            deviceSearchVo.setStartIndex(page.getStartIndex());
            page.setPage(true);
            page.setTotalCount(count);
            responseVos = this.selectHarmfulList(deviceSearchVo);
            page.setData(responseVos);
        }
        return page;
    }

    @Override
    public boolean editByCondition(MedicalDeviceSearchVo deviceSearchVo) {
        boolean flag = false;
        try{
            mapper.update(deviceSearchVo);
            flag = true;
        }catch (Exception e){
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteByCondition(MedicalDeviceSearchVo deviceSearchVo) {
        boolean flag = false;
        try{
            mapper.deleteByPrimaryKey(deviceSearchVo);
            flag = true;
        }catch (Exception e){
            flag = false;
        }
        return flag;
    }
}
